<html>
<head><meta charset="utf-8"><title>dynamic linking libstd with staticlib · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html">dynamic linking libstd with staticlib</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="240593744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240593744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240593744">(May 28 2021 at 11:57)</a>:</h4>
<p>In discussion about adopting rust for use in systemd it was brought up that dynamically linking libstd while producing a static library would be nice. Currently <code>--crate-type staticlib</code> in combination with <code>-Cprefer-dynamic</code> will bundle <code>libstd.rlib</code> into the resulting static library. To support dynamically linking there will need to be some way to prevent rustc from bundling it and to emit information to the build system about which dylibs need to be linked in.</p>



<a name="240595874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240595874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Boccassi <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240595874">(May 28 2021 at 12:19)</a>:</h4>
<p>From the interface point of view, given -Cprefer-dynamic is ignored when staticlib is used, would changing rustc to not ignore it suffice? It would avoid a new parameter, which means it's easier to integrate in Meson.<br>
Regarding emitting information, I don't really think anything special or new is needed. The libstd-rust package could simply ship a pkgconfig file, like every other shared library packages already do - that would make integration in Meson trivial.</p>



<a name="240598867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240598867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240598867">(May 28 2021 at 12:48)</a>:</h4>
<p>It would be a breaking change. I am not familiar with pkgconfig, but I think it could work for libstd. It won't work when using cargo to build the staticlib though.</p>



<a name="240608891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240608891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240608891">(May 28 2021 at 14:08)</a>:</h4>
<p>What is the advantage of using stdlib as a dynamic library? My understanding is everything generic is going to be generated and compiled into the static binary anyways, does dynamic linking help with non-generic methods and free functions that rust's std exports?</p>



<a name="240608909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240608909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Boccassi <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240608909">(May 28 2021 at 14:08)</a>:</h4>
<p>Ok, a new "crate-type" maybe? That way the integration with meson again should work without a new version. But not too bothered by how the switch is implemented/named/used in practice - just a suggestion.</p>
<p>I have no use case for cargo (quite the opposite: explicitly avoiding it), so not a problem for me. The .pc could even be added downstream, it's not a big deal.</p>



<a name="240611164"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240611164" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Boccassi <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240611164">(May 28 2021 at 14:24)</a>:</h4>
<p><span class="user-mention silent" data-user-id="257428">Gus Wynn</span> <a href="#narrow/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib/near/240608891">said</a>:</p>
<blockquote>
<p>What is the advantage of using stdlib as a dynamic library? My understanding is everything generic is going to be generated and compiled into the static binary anyways, does dynamic linking help with non-generic methods and free functions that rust's std exports?</p>
</blockquote>
<p>Two requirements:</p>
<p>1) Keep installation size sane - given the rust archive is a basic internal helper library, it's included in dozens of binaries. The size of the rust-enabled module is already 20 times larger than the existing one, multiply that by dozens of binaries and the end result becomes a bit silly.<br>
2) Allow the distros security teams to service libstd-rust like all other existing systemd dependencies, which are all shared libraries</p>
<p>Also, we don't do static binaries.</p>



<a name="240612835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240612835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240612835">(May 28 2021 at 14:36)</a>:</h4>
<blockquote>
<p>2) Allow the distros security teams to service libstd-rust like all other existing systemd dependencies, which are all shared libraries</p>
</blockquote>
<p>Dynamic linking rust doesn't really help with that. Too many functions are generic or marked as <code>#[inline]</code> and thus codegened in the target executable instead of <a href="http://libstd.so">libstd.so</a>. In addition there is no way to guarantee that the patched libstd source uses the same symbol names for the same functions. While it is currently pretty stable because incr comp needs it, this hasn't always been the case and may not always be the case. Rust doesn't have a stable ABI.</p>



<a name="240612847"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240612847" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240612847">(May 28 2021 at 14:37)</a>:</h4>
<p><span class="user-mention" data-user-id="416323">@Luca Boccassi</span> thanks for the background on how the systemd build works!<br>
I think what I was also wondering is, just like things like the STL which are heavily templated work with dynamic linking? My understanding is that the templated parts are compiled into the binary (as they exist in headers), and the rest is just normal dynamic linking? I think rust works the same way where "generics ~= templates", but I'd be interested in a compiler person could confirm! In that case, I'd be interested to keep up with the PR and see how much binary size is saved with dynamic linking of the stdlib</p>



<a name="240612881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240612881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240612881">(May 28 2021 at 14:37)</a>:</h4>
<p>ah, <span class="user-mention" data-user-id="133247">@bjorn3</span> answered some of my questions!</p>



<a name="240613203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240613203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240613203">(May 28 2021 at 14:39)</a>:</h4>
<p>Size win for hello world without LTO: 3.2MB -&gt; 19KB With LTO 1.9MB -&gt; not allowed. <a href="http://libstd.so">libstd.so</a> is 8MB. The overhead is pretty constant though.</p>



<a name="240615874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240615874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Boccassi <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240615874">(May 28 2021 at 14:58)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib/near/240612835">said</a>:</p>
<blockquote>
<blockquote>
<p>2) Allow the distros security teams to service libstd-rust like all other existing systemd dependencies, which are all shared libraries</p>
</blockquote>
<p>Dynamic linking rust doesn't really help with that. Too many functions are generic or marked as <code>#[inline]</code> and thus codegened in the target executable instead of <a href="http://libstd.so">libstd.so</a>. In addition there is no way to guarantee that the patched libstd source uses the same symbol names for the same functions. While it is currently pretty stable because incr comp needs it, this hasn't always been the case and may not always be the case. Rust doesn't have a stable ABI.</p>
</blockquote>
<p>How much stuff are we talking about? A few inline functions are common enough in shared libraries already, it wouldn't be a show-stopper. If it's half the standard library or something that is inlined though, it's an entirely different picture.<br>
I'm not worried about ABI stability - it's up to the distro security teams to figure out how to ship CVE fixes in a compatible way. What I need is to enable them to do so on my side, by avoiding to vendor stuff.</p>



<a name="240620651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240620651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240620651">(May 28 2021 at 15:31)</a>:</h4>
<p>Most vulnerabilities in the standard library are inside generic datastructures, generic iterator implementations or other generic functions it seems based on a quick look through all issues marked as I-unsound: <a href="https://github.com/rust-lang/rust/issues?q=is%3Aissue+label%3A%22I-unsound+%F0%9F%92%A5%22+is%3Aclosed+-label%3AICEBreaker-LLVM+-label%3Arequires-nightly+-label%3AA-llvm+-label%3AA-mir-opt">https://github.com/rust-lang/rust/issues?q=is%3Aissue+label%3A%22I-unsound+%F0%9F%92%A5%22+is%3Aclosed+-label%3AICEBreaker-LLVM+-label%3Arequires-nightly+-label%3AA-llvm+-label%3AA-mir-opt</a> (label exclusions are to exclude miscompilations caused by rustc or LLVM. As they could affect user code directly too.)</p>



<a name="240620770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240620770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240620770">(May 28 2021 at 15:32)</a>:</h4>
<p>there's staticlib-nobundle I think?</p>



<a name="240620773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240620773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240620773">(May 28 2021 at 15:32)</a>:</h4>
<p>does that work?</p>



<a name="240620862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240620862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240620862">(May 28 2021 at 15:32)</a>:</h4>
<p>That is for not bundling non-rust static archives AFAIK.</p>



<a name="240660069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/240660069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Boccassi <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#240660069">(May 28 2021 at 21:01)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib/near/240620651">said</a>:</p>
<blockquote>
<p>Most vulnerabilities in the standard library are inside generic datastructures, generic iterator implementations or other generic functions it seems based on a quick look through all issues marked as I-unsound: <a href="https://github.com/rust-lang/rust/issues?q=is%3Aissue+label%3A%22I-unsound+%F0%9F%92%A5%22+is%3Aclosed+-label%3AICEBreaker-LLVM+-label%3Arequires-nightly+-label%3AA-llvm+-label%3AA-mir-opt">https://github.com/rust-lang/rust/issues?q=is%3Aissue+label%3A%22I-unsound+%F0%9F%92%A5%22+is%3Aclosed+-label%3AICEBreaker-LLVM+-label%3Arequires-nightly+-label%3AA-llvm+-label%3AA-mir-opt</a> (label exclusions are to exclude miscompilations caused by rustc or LLVM. As they could affect user code directly too.)</p>
</blockquote>
<p>Got it - it's a start, I guess, better than nothing</p>



<a name="241385936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/241385936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Boccassi <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#241385936">(Jun 03 2021 at 12:52)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> is there an RFE for this I can reference? Thanks!</p>



<a name="241386064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/dynamic%20linking%20libstd%20with%20staticlib/near/241386064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/dynamic.20linking.20libstd.20with.20staticlib.html#241386064">(Jun 03 2021 at 12:53)</a>:</h4>
<p>No, not yet.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>